#include <stdio.h>
#include <stdlib.h>
#include <search.h>
#include <time.h>

int values[11];

int cmpfunc(const void *a, const void *b)
{
    return (*(int *)a - *(int *)b);
}

int main()
{
    int n, key = 312;
    srand((unsigned)time(NULL));
    for (n = 0; n < 11; n++)
    {
        values[n] = rand() % 1000 + 1;
    }
    values[10] = key;
    printf("排序之前的列表：\n");
    for (n = 0; n < 11; n++)
    {
        printf("%d ", values[n]);
    }

    qsort(values, 11, sizeof(int), cmpfunc);

    printf("\n排序之后的列表：\n");
    for (n = 0; n < 11; n++)
    {
        printf("%d ", values[n]);
    }
    printf("\n");

    int * res;
    size_t ns = 11;
    res = bsearch(&key, values, ns, sizeof(int), cmpfunc);
    if(res){
 
      printf("Number %d is found\n",key);
 
   }else{
 
      printf("Number %d is not found\n",key);
 
   }
    return 0;
}
